package com.zrrd.yunchmall.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.yunchmall.user.entity.Admin;
import com.zrrd.yunchmall.user.entity.AdminRoleRelation;
import com.zrrd.yunchmall.user.entity.Role;
import com.zrrd.yunchmall.user.mapper.AdminMapper;
import com.zrrd.yunchmall.user.mapper.AdminRoleRelationMapper;
import com.zrrd.yunchmall.user.mapper.MenuMapper;
import com.zrrd.yunchmall.user.mapper.RoleMapper;
import com.zrrd.yunchmall.user.service.IAdminService;
import com.zrrd.yunchmall.user.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@Service
@SuppressWarnings("all")
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleRelationMapper roleRelationMapper;

    @Override
    public String login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        //状态为0表示无法登录
        queryWrapper.eq("status", 1);
        Admin admin = getOne(queryWrapper);
        // userTmp == null 说明用户名不存在
        if (admin != null) {
            if (BCrypt.checkpw(password, admin.getPassword())) {
                //更新登录时间
                admin.setLoginTime(LocalDateTime.now());
                //update ums_admin set login_time = ? where id= ?
                UpdateWrapper updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("login_time", admin.getLoginTime());
                updateWrapper.eq("id", admin.getId());
                super.update(updateWrapper);
                return JwtUtil.create(24 * 30 * 60 * 1000, admin);
            }
        }
        // 用户名或密码错误则返回空
        return null;
    }

    @Override
    public Admin setPermissionInfo(Admin admin) {
        admin = getById(admin.getId());

        admin.setRoles(roleMapper.selectRolesByAdminId(admin.getId()));

        admin.setMenus(menuMapper.selectMenuByAdminId(admin.getId(), 1));

        return admin;
    }

    @Override
    public List<Role> getRoleList(long id) {
        return roleMapper.selectRolesByAdminId(id);
    }

    @Override
    @Transactional
    public void allocRole(Long adminId, String roleIds) {
        //1.移除全部角色
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", adminId);
        //delete from ums_admin_role_relation where admin_id=?
        roleRelationMapper.delete(queryWrapper);
        //2.给他分配当前角色
        if (!StringUtils.isEmpty(roleIds)){
            for (String roleId : roleIds.split(",")) {
                AdminRoleRelation relation=new AdminRoleRelation();
                relation.setAdminId(adminId);
                relation.setRoleId(Long.valueOf(roleId));
                roleRelationMapper.insert(relation);
            }
        }

        //3.将1，2置于一个事物之中
    }
}
